<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\News;
use DB; 

class NewController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $news = News::where(function($query) use ($request){
            //检测参数
            if(!empty($request->title)) {
                $query -> where('title','like','%'.$request->title.'%');
            }

            if(!empty($request->status)) {
                $query->where('status', $request->status);
            }

        })->orderBy('id','desc')->paginate($request->input('num',10));
        return view('admin.new.index',['news'=>$news,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
        return view('admin.new.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 表单验证

        // 实例化对象
        $new = new News;
        $new->title = $request->title;
        $new->content = $request->content;
        $new->status = $request->status;

        if($new->save()){
            return redirect('/new');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $news = News::find($id);
        if(empty($news))  abort(404);

        // //解析模板
        return view('home.news.index', ['news'=>$news]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $new = News::findOrFail($id);

        // 显示模板
        return view('admin.new.edit',['new'=>$new]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       
        //实例化模型对象
        $news = news::findOrFail($id);

        // dd($id);
        $news->title = $request->title;
        $news->status = $request->status;
        $news->content = $request->content;
       
        if($news->save()){
            return redirect('/new');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        ////获取数据
        $new = News::findOrFail($id);

        if($new->delete()){
            return back()->with('success','执行成功');
        }else{
            return back()->with('error','执行失败');
        }
    }

}
